package toubiao.utils.filter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import utils.UtilCollection;
import utils.UtilController;
import utils.UtilString;


    /**
    * @ClassName: HqlBuilder
    * @Description: 保存生成 Hql 语句 相关信息
    * @author 宁少林
    * @date 2016年9月12日
    *
    */
public class HqlBuilder {

	
	
	
	private  static Logger logger=LoggerFactory.getLogger(HqlBuilder.class);



	
	
	    /**
	    * @Title: buildHql
	    * @Description: 生成Hql,其中1.如果base 已经带了where语句的话，需要根据逻辑在后面 增加 and 或者 or 2.如果没带where的话，filter自己添加where
	    * @param @param filterConditionList
	    * @param @param orderConditionList
	    * @param @return    
	    * @return String    
	    * @throws
	    */
	public static String buildHql(String baseHql,Convert convert,List<FilterCondition> filterConditionList,List<OrderCondition> orderConditionList){
		String hql=baseHql;
		logger.info("base hql = "+hql);
		//hql=hql.toLowerCase();
		String filterHql=FilterHqlFactory.buildFilterHqlByList(filterConditionList, convert.getWebClassMap());
		if(!UtilString.isNullOrEmpty(filterHql)){
			if(baseHql.indexOf("where")>-1){
				hql=hql+" "+filterHql;
			}else{
				hql=hql+" where "+filterHql;
			}
		}
		logger.info("filter hql = "+hql);
		
		String orderHql="";
		if(!UtilCollection.isNullOrEmpty(orderConditionList)){
			orderHql=OrderHqlFactory.buildOrderHqlByList(orderConditionList, convert.getWebClassMap());
		}
		hql=hql+orderHql;
		logger.info("hql = "+hql);
		return hql;
	}
}
